﻿// <copyright file="EsriPolyline.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>Implementation of EsriPolyline class.</summary>

namespace BESASM.EMGAATS.Arc
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using BESASM.EMGAATS.BusinessClasses;
  using ESRI.ArcGIS.esriSystem;
  using ESRI.ArcGIS.Geometry;

  /// <summary>
  /// An implementation of IPolyline which wraps an ESRI Polyline object.
  /// </summary>
  public class EsriPolyline : BusinessClasses.IPolyline
  {
    private ESRI.ArcGIS.Geometry.IPolyline esriPolyline;

    /// <summary>
    /// Initializes a new instance of the EsriPolyline class.
    /// </summary>
    /// <param name="polyline">The ESRI polyline this class will wrap.</param>
    public EsriPolyline(ESRI.ArcGIS.Geometry.IPolyline polyline)
    {
      this.esriPolyline = polyline;
    }

    /// <summary>
    /// Gets the ESRI Polyline this object wraps.
    /// </summary>
    public ESRI.ArcGIS.Geometry.IPolyline Polyline
    {
      get
      {
        return this.esriPolyline;
      }
    }

    #region IPolyline Members

    /// <summary>
    /// Gets or sets the length of the polyline
    /// </summary>
    public double Length
    {
      get
      {
        return this.esriPolyline.Length;
      }

      set
      {
        throw new NotSupportedException(string.Format("Cannot set length of {0} directly.", this.GetType().Name));
      }
    }

    /// <summary>
    /// Returns a value indicating if this polyline has the same coordinates as the given polyline.
    /// </summary>
    /// <param name="polyline">The polyline to compare to this one.</param>
    /// <returns>True if the polylines are the same, otherwise false.</returns>
    public bool IsEquivalentTo(BusinessClasses.IPolyline polyline)
    {
      if (polyline == null)
      {
        return false;
      }

      if (polyline is EsriPolyline)
      {
        IRelationalOperator otherPolyline = ((EsriPolyline)polyline).Polyline as IRelationalOperator;
        return otherPolyline.Equals(this.esriPolyline);
      }
      else
      {
        throw new NotSupportedException("Cannot compare polylines that are not EsriPolylines.");
      }
    }

    /// <summary>
    /// Creates a new polyline that is a copy of the current instance.
    /// </summary>
    /// <returns>A new object that is a copy of this instance.</returns>
    public object Clone()
    {
      IClone lineClone = ((IClone)this.esriPolyline).Clone();
      return new EsriPolyline((ESRI.ArcGIS.Geometry.IPolyline)lineClone);
    }

    #endregion
  }
}
